﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using log4net;

namespace MvcForum.Net.Bussiness
{
    public class BaseController : System.Web.Mvc.Controller
    {

        protected static readonly ILog log = LogManager.GetLogger(typeof(BaseController));

        protected override void OnException(ExceptionContext filterContext)
        {

            if (filterContext == null)
            {

                throw new ArgumentNullException("filterContext");

            }


            var controllerName = (string)filterContext.RouteData.Values["controller"];

            var actionName = (string)filterContext.RouteData.Values["action"];

            var _error = new StringBuilder();

            _error.AppendLine("");
            _error.AppendFormat("Controller: {0}{1}", controllerName, Environment.NewLine);
            _error.AppendFormat("Action: {0}{1}",actionName, Environment.NewLine);
            if (filterContext.RequestContext.HttpContext.Request.Url != null)
                _error.AppendLine(filterContext.RequestContext.HttpContext.Request.Url.ToString());
            _error.AppendLine(filterContext.Exception.ToString());

            log.Error(_error.ToString());

            if (ForumContext.Configuration.AutoFeedbackErrorException)
            {

                EmailQueueRepo.Save(ForumContext.Configuration.SmtpSender, "admin@mvcforum.net",
                                       "System Exception", filterContext.RequestContext.HttpContext.Request.Url + Environment.NewLine + filterContext.Exception, EmailPriority.Hight);
            }

            if (!filterContext.ExceptionHandled && filterContext.HttpContext.IsCustomErrorEnabled)
            {



                var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);


                /*  var result = new ViewResult
                                   {
                                     

                                       ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
                                       TempData = filterContext.Controller.TempData
                                   };


                  filterContext.Result = result;*/

                filterContext.ExceptionHandled = true;

                filterContext.HttpContext.Response.Clear();

                filterContext.HttpContext.Response.StatusCode = 500;

                filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;

                View("Error", model).ExecuteResult(ControllerContext);


            }
        }





        protected override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            var _request = filterContext.HttpContext.Request;

            if (ForumContext.Configuration.AllowClientActivityLogging)
            {

                var _m = new StringBuilder();

                _m.Append(ForumContext.ClientIp);
                _m.AppendLine("");
                _m.AppendLine(ForumContext.Logined ? ForumContext.UserInfo.UserName : string.Empty);
                _m.AppendLine(_request.RawUrl);
                log.Info(_m.ToString());
            }


            if (!_request.IsAjaxRequest() && _request.RequestType.ToUpper() == "GET")
                ForumContext.ClientActivity(filterContext.Controller.ViewBag.ActiveUrl);
        }


    }
}
